home *** CD-ROM | disk | FTP | other *** search
- # Source Generated with Decompyle++
- # File: in.pyc (Python 2.6)
-
- from OpenGL.GL import *
- from OpenGL.GLU import *
- import png
- import array
-
- class Texture:
- '''
- '''
- __data = None
- __format = GL_RGB
- __width = None
- __height = None
- __ambient = None
- __diffuse = None
- __specular = None
- __emission = None
- __shininess = None
- __texture = None
-
- def __init__(self, fileName, ambient = (0.2, 0.2, 0.2, 1), diffuse = (0.8, 0.8, 0.8, 1), specular = (0, 0, 0, 1), emission = (0, 0, 0, 1), shininess = 0):
- """Constructor for an openGL texture.
-
- 'fileName' is the name of the image file to use for the texture (string).
-
- An IOError is raised if the file does not exist.
- This does not need an openGL context.
- """
- self._Texture__ambient = ambient
- self._Texture__diffuse = diffuse
- self._Texture__specular = specular
- self._Texture__emission = emission
- self._Texture__shininess = shininess
-
- try:
- self._Texture__loadPIL(fileName)
- except ImportError:
- self._Texture__loadPNG(fileName)
-
-
-
- def __loadPNG(self, fileName):
- '''
- '''
-
- try:
- reader = png.Reader(fileName)
- except IOError:
- e = None
- print 'Error loading texture file: %s: %s' % (fileName, e.strerror)
- self._Texture__data = None
- return None
-
-
- try:
- (width, height, data, metaData) = reader.read()
- except png.Error:
- e = None
- print 'Error parsing PNG file %s: %s' % (fileName, e.message)
- self._Texture__data = None
- return None
-
- self._Texture__width = width
- self._Texture__height = height
- self._Texture__data = array.array('B', data).tostring()
- if metaData['has_alpha']:
- self._Texture__format = GL_RGBA
- else:
- self._Texture__format = GL_RGB
-
-
- def __loadPIL(self, fileName):
- '''
- '''
- import Image
-
- try:
- image = Image.open(fileName)
- except IOError:
- e = None
- print 'Error loading texture file: %s: %s' % (fileName, e.strerror)
- self._Texture__data = None
- return None
-
- width = image.size[0]
- height = image.size[1]
- w = 1
- while 2 * w <= width:
- w *= 2
- h = 1
- while 2 * h <= height:
- h *= 2
- self._Texture__width = w
- self._Texture__height = h
- image = image.crop((0, 0, w, h))
- self._Texture__data = image.tostring('raw', 'RGB', 0, -1)
- self._Texture__format = GL_RGB
-
-
- def __generate(self):
- '''
- '''
- if self._Texture__data is None:
- return 0
- texture = glGenTextures(1)
- glBindTexture(GL_TEXTURE_2D, texture)
- glPixelStorei(GL_UNPACK_ALIGNMENT, 1)
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT)
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT)
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR)
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR)
-
- try:
- gluBuild2DMipmaps(GL_TEXTURE_2D, GL_LUMINANCE, self._Texture__width, self._Texture__height, self._Texture__format, GL_UNSIGNED_BYTE, self._Texture__data)
- except GLUerror:
- self._Texture__data is None
- e = self._Texture__data is None
- glTexImage2D(GL_TEXTURE_2D, 0, 3, self._Texture__width, self._Texture__height, 0, self._Texture__format, GL_UNSIGNED_BYTE, self._Texture__data)
- except:
- self._Texture__data is None
-
- return texture
-
-
- def bind(self):
- '''Bind this texture to the current surface.
-
- This requires an openGL context.
- '''
- if self._Texture__texture is None:
- self._Texture__texture = self._Texture__generate()
- self._Texture__data = None
-
- glBindTexture(GL_TEXTURE_2D, self._Texture__texture)
- glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, self._Texture__ambient)
- glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, self._Texture__diffuse)
- glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, self._Texture__specular)
- glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, self._Texture__emission)
- glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, self._Texture__shininess)
-
-
-